<?php
require_once('../../../../conf/config.php.inc');
require_once(SYBMAS_LIB_PATH.'/liblogin.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/layout.php.inc');
require_once(SYBMAS_LIB_PATH.'/libsybmas.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/menu.php.inc');
require_once('../../lib/libtablas.php.inc');
include_once('otorgarRevocar.php.inc');
verificar_inicio_sesion();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/sybmasstyle.css">
<script src="<?php echo LAYOUT_URI?>/jquery-1.2.1.min.js"
	type="text/javascript"></script>
<script src="<?php echo LAYOUT_URI?>/menu-collapsed.js"
	type="text/javascript"></script>
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/style.css" >
<link rel="stylesheet" type="text/css"
	href="<?php echo JAVASCRIPT_URI?>/tabs/tabs.css">
<script src="<?php echo JAVASCRIPT_URI?>/tabs/tabs.js"
	type="text/javascript"></script>
<script type="text/javascript" src="<?php echo JAVASCRIPT_URI?>/commons.js"></script>
<title>.:: Sybmas - Agregar columna</title>
<!--[if lt IE 8]>
   <style type="text/css">
   li a {display:inline-block;}
   li a {display:block;}
   </style>
   <![endif]-->
</head>
<body >  
<script src="<?php echo JAVASCRIPT_URI?>/tooltip/wz_tooltip.js"
	type="text/javascript"></script>
<div id="dialog">
<div class="title">Privilegios sobre objetos</div>

<?php 
$dbname=false;
foreach($_POST as $key => $value){
	$$key=$value;
	//echo "$key=$value<br>";
}
?>
<?php
$cambio_bd_exitoso=true;
//sybase_min_server_severity(11);
$sybuser_connection=start_default_sybuser_connection();
if($dbname){
	$cambio_bd_exitoso=sybase_select_db($dbname,$sybuser_connection);
}
sybase_set_message_handler("msg_handler",$sybuser_connection);
if($cambio_bd_exitoso){
	$messages=null;
	if(isset($allPrivilegesList) && count($allPrivilegesList)>0){	
		$query="REVOKE ";
		$revokedPrivilegesList="";
		
		$isFirstLoop=true;
			foreach($allPrivilegesList as $privilege){
				if(!$isFirstLoop){
					$revokedPrivilegesList.=",";
				}
	
				$revokedPrivilegesList.="$privilege ";
				$isFirstLoop=false;
			}
		$query.="$revokedPrivilegesList ";
		$query.="ON $tableowner.$tablename FROM $grantTo ";
		
		//sybase_set_message_handler("void_msg_handler",$sybuser_connection);
		$result=@sybase_query($query,$sybuser_connection);
		//echo $query;
		foreach($allPrivilegesList as $privilege){
				insertIntoLog($grantTo,'REVOKE',$privilege,$dbname,$id);
		}
		
		$messages.="<h3>Privilegios revocados: $revokedPrivilegesList</h3>";		
	}
	
	if( (isset($privileges) && count($privileges)>0) || isset($allPrivileges)){
		$grantedPrivilegesList=null;
		$query="GRANT  ";	
		if(isset($allPrivileges)){		
			$grantedPrivilegesList='ALL ';
		}else{	
			$isFirstLoop=true;
			foreach($privileges as $privilege){
				if(!$isFirstLoop){
					$grantedPrivilegesList.=",";
				}
	
				$grantedPrivilegesList.="$privilege ";
				$isFirstLoop=false;
			}
		}
		
		$query.=$grantedPrivilegesList;
		$query.="ON $tableowner.$tablename ";
		$query.="TO $grantTo ";
		$query.=isset($withGrantOption)?"WITH GRANT OPTION ":"";
		
		//sybase_set_message_handler("void_msg_handler",$sybuser_connection);
		sybase_select_db($dbname);
		$result=sybase_query($query,$sybuser_connection);
		//echo $query;
		
		if(isset($allPrivileges)){
			insertIntoLog($grantTo,'GRANT','ALL',$dbname,$id);
		}else{
			foreach($privileges as $privilege){
				insertIntoLog($grantTo,'GRANT',$privilege,$dbname,$id);
			}
		}
		$messages.="<h3>Privilegios otorgados: $grantedPrivilegesList</h3>";
		sybase_set_message_handler("msg_handler",$sybuser_connection);
	}
	
	
	if($result){
		echo "<h1>La modificacion de privilegios fue exitosa</h1>";	
	}else{
		echo "<h1>Error modificacion de privilegios</h1>";
		echo "<h4>".sybase_get_last_message()."</h4>";
	}
	echo $messages;
}//if($cambio_bd_exitoso)

//Cerrando conexiones a la base de datos
sybase_close($sybuser_connection);
?>	
<input type="button" value="Aceptar" onclick="window.close();">	
</div>
</body>
</html>